package com.meida.module.bus.provider.service.impl;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.module.bus.client.entity.BusOrderAppraise;
import com.meida.module.bus.provider.mapper.BusOrderAppraiseMapper;
import com.meida.module.bus.provider.service.BusOrderAppraiseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * 订单评价表接口实现类
 *
 * @author flyme
 * @date 2021-08-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusOrderAppraiseServiceImpl extends BaseServiceImpl<BusOrderAppraiseMapper, BusOrderAppraise> implements BusOrderAppraiseService {


    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusOrderAppraise boa, EntityMap extra) {
        Long userId= OpenHelper.getUserId();
        boa.setAppraiseUserId(userId);
        return ResultBody.ok();
    }

    @Override
    public ResultBody afterAdd(CriteriaSave cs, BusOrderAppraise busOrderAppraise, EntityMap extra) {
        return ResultBody.ok("评价成功",1);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusOrderAppraise> cq, BusOrderAppraise boa, EntityMap requestMap) {
        cq.orderByDesc("orderAppraise.createTime");
        return ResultBody.ok();
    }

    @Override
    public EntityMap getTotalScoreInfo(Long answerUserId) {
        CriteriaQuery cq = new CriteriaQuery(BusOrderAppraise.class);
        cq.select("COALESCE(COUNT(*),0) totalNum", "COALESCE(SUM(score),0) totalScore");
        cq.eq(true,"answerUserId", answerUserId);
        return findOne(cq);
    }

    @Override
    public Integer countAppraiseNum(Long answerUserId) {
        CriteriaQuery cq = new CriteriaQuery(BusOrderAppraise.class);
        cq.select("COALESCE(COUNT(DISTINCT appraiseUserId),0) appraiseNum");
        cq.eq("answerUserId", answerUserId);
        EntityMap entityMap = findOne(cq);
        return entityMap.getInt("appraiseNum");
    }

    @Override
    public Long countTotalAppraiseNum(Long answerUserId) {
        CriteriaQuery cq = new CriteriaQuery(BusOrderAppraise.class);
        cq.eq("answerUserId", answerUserId);
        return count(cq);
    }

    @Override
    public ResultBody getWaitOrderAppraiseService(Long answerUserId) {

        return null;
    }

    @Override
    public Long countNumByOrderId(Long orderId) {
        CriteriaQuery cq = new CriteriaQuery(BusOrderAppraise.class);
        cq.eq("orderId", orderId);
        return count(cq);
    }

    @Override
    public EntityMap getOrderAppraiseInfo(Long orderId) {
        CriteriaQuery cq = new CriteriaQuery(BusOrderAppraise.class);
        cq.select("appraiseId", "appraiseContent", "score", "createTime");
        cq.eq("orderId", orderId);
        EntityMap entityMap = findOne(cq);
        return entityMap;
    }
}
